package com.example.game.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.game.common.BaseServiceImpl;
import com.example.game.dao.bo.AdminHarderBO;
import com.example.game.dao.bo.QueryAdminBO;
import com.example.game.dao.po.GenshenAdmin;
import com.example.game.mapper.AdminMapper;
import com.example.game.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CHENTAIWEN
 * @create 2023/2/17 9:48
 */
@Slf4j
@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminMapper, GenshenAdmin> implements AdminService {


    @Override
    public AdminHarderBO queryAdmin(QueryAdminBO queryAdminBO) {
        AdminHarderBO harder = queryUser();
        return harder;
    }

    @Override
    public String roleArms() {
        Map<String, Object> map = queryRoleArms();
        List roleList = JSON.parseObject(map.get("role").toString(), List.class);
        List armsList = JSON.parseObject(map.get("arms").toString(), List.class);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("带");
        stringBuilder.append(armsList.get((int) (Math.random() * 35)));
        stringBuilder.append("的");
        stringBuilder.append(roleList.get((int) (Math.random() * 66)));
        return stringBuilder.toString();
    }


    /**
     * 获取到表信息
     */
    private AdminHarderBO queryUser() {
        try {
            ClassPathResource classPathResource = new ClassPathResource("org/type");
            StringBuilder sb = new StringBuilder();
            InputStreamReader bufferedInputStream = new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8);
            int tmp = 0;
            while ((tmp = bufferedInputStream.read()) != -1) {
                sb.append((char) tmp);
            }
            String json = sb.toString();
            HashMap hashMap = JSON.parseObject(json, HashMap.class);
            return JSON.parseObject(json, AdminHarderBO.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取到表信息
     */
    private Map<String, Object> queryRoleArms() {
        try {
            ClassPathResource classPathResource = new ClassPathResource("org/role");
            StringBuilder sb = new StringBuilder();
            InputStreamReader bufferedInputStream = new InputStreamReader(classPathResource.getInputStream(), StandardCharsets.UTF_8);
            int tmp = 0;
            while ((tmp = bufferedInputStream.read()) != -1) {
                sb.append((char) tmp);
            }
            String json = sb.toString();
            return JSON.parseObject(json, HashMap.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
